﻿@{
    ViewBag.Title = "病历预览";
    Layout = "~/Views/Shared/_Index.cshtml";
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script src="~/Scripts/jquery.min.js"></script>
</head>
<body>
    <div>
        <form method="post" action="#">
            <div style="width:100%;height:100%">
                @Html.Raw(ViewBag.WriterControlHtml)
            </div>
        </form>
    </div>

</body>
</html>

<script>
        var ctl = document.getElementById("myWriterControl");
        ctl.style.height = "800px";
        ctl.Options.workspacebackcolorstring = "#66CCCC"
        //打开文档
        function FileOpen(fileUrl) {
            var ctl = document.getElementById("myWriterControl");
            var doc = ctl.GetContentDocument();
            $(doc).find("[dctype='XTextDocumentHeaderElement'],[dctype='XTextDocumentFooterElement'],[dctype='XTextDocumentHeaderForFirstPageElement'],[dctype='XTextDocumentFooterForFirstPageElement']").html("");
            ctl.ClearDocumentBody();
            ctl.preserveScrollPosition = true;
            var fileName = fileUrl;//"//File//住院病历//2023//12//01//03245//011028患者告知书.xml";
            //在服务器端响应控件的EventReadFileContent事件，即可使用AJAX模式加载文档。
            var result = ctl.LoadDocumentFromFile(fileName, null);
            //ctl.ServerMessage 服务端错误返回
            setTimeout(function () { PrintPreview(); }, 300)
            
        }
        //打印
        function print() {
            var ctl = document.getElementById("myWriterControl");
            ctl.PrintDocument();

        }
        //打印预览
        function PrintPreview() {
            var ctl = document.getElementById("myWriterControl");
            ctl.LoadPrintPreview();
        }
        var ctl = document.getElementById("myWriterControl");
        function Build() {
            if (ctl) {
                BindingDCWriterClientControl(ctl);
                ctl.EventAfterPrintPreview = function (PrintPreviewDoc) {
                    var styleDom = document.createElement("style");
                    // 解决宋体打印问题
                    var cssStr = "@@font-face {font-family: 宋体;src: local('宋体');}";
                    styleDom.innerHTML = cssStr;
                    var page = PrintPreviewDoc.querySelector("div[pageindex]");
                    if (page) {
                        page.appendChild(styleDom);
                    }

                }
            }
            FileOpen();
        }
        Build();

</script>